﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class AboutMuseum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlCountry.DataSource = DBAccess.GetAllCountries();
            ddlCountry.DataBind();

            ddlCountry.Items.FindByText("United States").Selected = true;

            //load cities from DB
            ddlCity.DataSource = DBAccess.GetCities(Convert.ToInt32(ddlCountry.SelectedValue), DateTime.Parse(Constants.CityMinDate));
            ddlCity.DataBind();

            BindMuseumData();
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindMuseumData()
    {
        DataTable dtMuseum = new DataTable();
        try
        {
            if (Request.QueryString["MuseumID"] != null)
            {
                dtMuseum = DBAccess.GetMuseumById(Convert.ToInt32(Request.QueryString["MuseumID"]));
                Session["MuseumID"] = Request.QueryString["MuseumID"];
            }
            else if (Session["MuseumId"] != null)
                dtMuseum = DBAccess.GetMuseumById(Convert.ToInt32(Session["MuseumId"]));

            if (dtMuseum != null && dtMuseum.Rows.Count > 0)
            {
                txtMuseumName.Text = dtMuseum.Rows[0]["MuseumName"].ToString();
                txtAddress.Text = dtMuseum.Rows[0]["MuseumAddress"].ToString();
                txtDetails.Text = dtMuseum.Rows[0]["MuseumInfo"].ToString();
                txtMuseumLink.Text = dtMuseum.Rows[0]["MuseumLink"].ToString();
                ddlCountry.SelectedValue = dtMuseum.Rows[0]["CountryId"].ToString();

                //load cities from DB
                ddlCity.DataSource = DBAccess.GetCities(Convert.ToInt32(ddlCountry.SelectedValue), DateTime.Parse(Constants.CityMinDate));
                ddlCity.DataBind();

                ddlCity.SelectedValue = dtMuseum.Rows[0]["CityId"].ToString();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int museumId = 0;
        int result = 0;
        if (Session["MuseumID"] != null)
        {
            museumId = Convert.ToInt32(Session["MuseumID"]);
            result = DBAccess.UpdateMuseumDetails(museumId, txtMuseumName.Text, txtAddress.Text, txtMuseumLink.Text, txtDetails.Text);
            if (result == -1)
            {
                lblResult.Text = "Museum details saved successfully.";
                lblResult.Visible = true;
            }
            else
            {
                lblResult.Text = "Error in saving Museum details.";
                lblResult.Visible = true;
            }
        }
        else
        {
            lblResult.Text = "Error in saving Museum details.";
            lblResult.Visible = true;
        }
    }
}
